package com.moji.common.aop;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import com.moji.common.base.ApiResult;
import com.moji.common.config.OperateLog;
import com.moji.common.domain.CcpOperateLog;
import com.moji.common.service.CommonCcpLogService;
import com.moji.common.utils.IPAddressUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@Aspect
@Component
public class LogAspect {

    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);

    private final long successCode = 1;

    @Resource
    private CommonCcpLogService ccpLogService;

    @Pointcut("@annotation(com.moji.common.config.OperateLog)")
    public void logPointCut(){

    }
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable{

        /**
         * 获取操作人相关信息 TODO
         */

        ApiResult obj = null;
        try {
            obj = (ApiResult) point.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
            log.error("统一处理操作日志异常",e);
            return point.proceed();
        }
        //执行成功统一保存操作日志信息
        if(successCode==obj.getCode()){
            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method = signature.getMethod();

            OperateLog annotation = method.getAnnotation(OperateLog.class);
            //系统编号
            String systemNumber = annotation.systemNumber();
            //系统名称
            String systemName = annotation.systemName();
            //模块编号
            String moduleNumber = annotation.moduleNumber();
            //模块名称
            String moduleName = annotation.moduleName();
            //操作类型
            String operateType = annotation.operateType();
            //内容
            String content = annotation.content();
            Map methodParamsMap = getMethodParams(point);
            //操作内容(接口入参)
            String jsonParams = JSON.toJSONString(methodParamsMap);
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String ipAdrress = IPAddressUtils.getIpAdrress(request);
            CcpOperateLog ccpLog = new CcpOperateLog();
            ccpLog.setSystemNumber(systemNumber);
            ccpLog.setSystemName(systemName);
            ccpLog.setModuleNumber(moduleNumber);
            ccpLog.setModuleName(moduleName);
            ccpLog.setOperateType(operateType);
            ccpLog.setContent(content);
            ccpLog.setOprContent(jsonParams);
            ccpLog.setIp(ipAdrress);
            ccpLogService.saveData(ccpLog);
        }
        return obj;
    }

        /**
         *  获取请求方法的参数
         * @param joinPoint
         */
        public  Map getMethodParams(JoinPoint joinPoint){
            String[] names = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
            Map params =new HashMap();
            if (ArrayUtils.isEmpty(names)) return params;
            Object[] values = joinPoint.getArgs();
            for (int i = 0; i < names.length; i++) {
                params.put(names[i],values[i]);
            }
            return params;
        }
}
